using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace lt_pmbc_001.Forms
{
	#region using
	using DevComponents.DotNetBar;
	using lt_pmbc_001.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
    using lt_pmbc_001.DAL;
    using lt_pmbc_001.DTO;
	#endregion

	public partial class FrmCapNhatThietBi : Office2007Form
	{
		public EventHandler Handler { get; set; }
		private DataTable DataSource;
		private int IDDiaDiem;

		public FrmCapNhatThietBi(int iddiadiem)
		{
			InitializeComponent();
			this.IDDiaDiem = iddiadiem;
		}

		private void FrmLapDiaDiem_Load(object sender, EventArgs e)
		{
			InitGridView();
			InitDataSource();
            LoadDiaDiem();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID",
				DataPropertyName = "ID",
                Name = "ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Phòng",
				DataPropertyName = "Phong",
				Name = "Phong",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Lầu",
				DataPropertyName = "Lau",
				Name = "Lau",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Tòa nhà",
				DataPropertyName = "ToaNha",
				Name = "ToaNha",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4)) - 1
			};
			dtgvData.Columns.Add(objColumn);
		}

		public void InitDataSource()
		{
			DataSource = new DataTable();
			DataSource.Columns.Add("ID");
			DataSource.Columns.Add("Phong");
			DataSource.Columns.Add("Lau");
			DataSource.Columns.Add("ToaNha");
			
            DataView objTimeSheetDV = new System.Data.DataView(DataSource);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}


		private void btnThem_Click(object sender, EventArgs e)
		{
			FrmCapNhatChiTietDiaDiem frm = new FrmCapNhatChiTietDiaDiem(IDDiaDiem);
			frm.Handler += GetChiTietDiaDiem;
			frm.ShowDialog();
		}

		public void GetChiTietDiaDiem(object sender, EventArgs e)
		{
			var eventType = (TransferDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
            var objBUS = new ChiTietDiaDiemDAL(GlobalVariables.g_strPath);
            var ds = objBUS.SelectByID(comp[0]);
            var row = ds.Tables[0].Rows[0];
            if (row != null)
			{
                DataSource.Rows.Add(comp[0], comp[1], comp[2], comp[3]);
			}
		}


		private void btnXoa_Click(object sender, EventArgs e)
		{
            if (DataSource.Rows.Count > 0 && dtgvData.SelectedRows.Count > 0)
			{
                var id = dtgvData.SelectedRows[0].Cells["ID"].Value.ToString();
                var objBUS = new ChiTietDiaDiemDAL(GlobalVariables.g_strPath);
                if (objBUS.Delete(id))
                {
                    DataSource.Rows.RemoveAt(dtgvData.SelectedRows[0].Index);
                }
			}
		}

		private bool ValidateInput(ref string errorText)
		{
			if (DataSource.Rows.Count == 0)
			{
				errorText = "Danh sách hiện thời không có gì!";
				return false;
			}
			return true;
		}

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void LoadDiaDiem()
		{
            var objBUS = new DiaDiemDAL(GlobalVariables.g_strPath);
            var ds = objBUS.SelectByID(IDDiaDiem);
			if (ds != null && ds.Tables[0].Rows.Count > 0)
			{
                var row = ds.Tables[0].Rows[0];
                txtID.Text = row["ID"].ToString();
                txtDiaDiem.Text = row["DiaDiem"].ToString();
				txtDiaChi.Text = row["DiaChi"].ToString();

                LoadChiTietDiaDiem(int.Parse(txtID.Text));
			}
		}

		private void LoadChiTietDiaDiem(int iddiadiem)
		{
            var objBUS = new ChiTietDiaDiemDAL(GlobalVariables.g_strPath);
            var ds = objBUS.SelectByIDDiaDiem(iddiadiem);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
			{
                foreach (DataRow row in ds.Tables[0].Rows)
				{
                    DataSource.Rows.Add(row["ID"].ToString(), row["Phong"].ToString(), row["Lau"].ToString(), row["ToaNha"].ToString());
				}
			}
		}

	}

}
